﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[Serializable]
public class NPCs
{
    public bool FirstZoom = false;

    public string ZoomText;

    public string name;

    public Sprite Head;

    public talk[] talk;

    public GameObject content;

    public GameObject talkPlane;
}
[Serializable]
public class talk
{
    public string Question;

    public string Answer;

    public Sprite Qs;

    public Sprite As;
}
public class NPCManager : MonoBehaviour
{
    public NPCs[] NPC;

    public GameObject Cell1;

    public GameObject Cell2;

    public GameObject content1;

    [HideInInspector]
    public int a;

    void Start()
    {
        for (int i = 0; i < NPC.Length; i++)//创建联系人
        {
            Cell1.GetComponent<CellManager>().name = NPC[i].name;
            Cell1.GetComponent<CellManager>().head = NPC[i].Head;
            GameObject G = Instantiate(Cell1, content1.transform) as GameObject;
            if (NPC[i].FirstZoom)
            {
                gameObject.GetComponent<Guide>().GuidePlay(G, NPC[i].ZoomText);
                G.GetComponent<CellManager>().IsNext = true;
            }
        }
    }

    public void Talks(string name)//剧情衔接程序
    {
        StartCoroutine(Talk(name));
    }

    IEnumerator Talk(string name)//剧情生成程序
    {
        Find(name);

        GameObject.Find("GameManager").GetComponent<DesktableManager>().JuQing((NPC[a].talk.Length * 4) + 1);//调用剧情模式，发出时间

        Clear(a);//先清空

        for (int k = 0; k < NPC[a].talk.Length; k++)//生成
        {
            if (k == 0) yield return new WaitForSeconds(2f);
            //问
            if (NPC[a].talk[k].Question != "0")//判断是否有内容
            {
                Cell2.GetComponent<TalkManager>()._内容 = NPC[a].talk[k].Question;
                Cell2.GetComponent<TalkManager>().head = NPC[a].Head;
                Cell2.GetComponent<TalkManager>().IsPlayer = true;
                Cell2.GetComponent<TalkManager>().IsPicture = false;
                Instantiate(Cell2, NPC[a].content.transform);
                yield return new WaitForSeconds(2f);
            }
            else
            {
                if (NPC[a].talk[k].Qs != null)
                {
                    Cell2.GetComponent<TalkManager>()._内容 = "";
                    Cell2.GetComponent<TalkManager>().head = NPC[a].Head;
                    Cell2.GetComponent<TalkManager>().IsPlayer = true;
                    Cell2.GetComponent<TalkManager>().IsPicture = true;
                    Cell2.GetComponent<TalkManager>().picture.sprite = NPC[a].talk[k].Qs;
                    Instantiate(Cell2, NPC[a].content.transform);
                    yield return new WaitForSeconds(2f);
                }
            }
            //回答
            if (NPC[a].talk[k].Answer != "0")//判断是否有内容
            {
                Cell2.GetComponent<TalkManager>()._内容 = NPC[a].talk[k].Answer;
                Cell2.GetComponent<TalkManager>().head = NPC[a].Head;
                Cell2.GetComponent<TalkManager>().IsPlayer = false;
                Cell2.GetComponent<TalkManager>().IsPicture = false;
                Instantiate(Cell2, NPC[a].content.transform);
                yield return new WaitForSeconds(2f);
            }
            else
            {
                if (NPC[a].talk[k].As != null)
                {
                    Cell2.GetComponent<TalkManager>()._内容 = "";
                    Cell2.GetComponent<TalkManager>().head = NPC[a].Head;
                    Cell2.GetComponent<TalkManager>().IsPlayer = false;
                    Cell2.GetComponent<TalkManager>().IsPicture = true;
                    Cell2.GetComponent<TalkManager>().picture.sprite = NPC[a].talk[k].As;
                    Instantiate(Cell2, NPC[a].content.transform);
                    yield return new WaitForSeconds(2f);
                }
            }
        }
    }

    public void Find(string name)
    {
        for (int k = 0; k < NPC.Length; k++)
        {
            if (name == NPC[k].name)
            {
                a = k;
                break;
            }
        }
    }

    public void Clear(int a)
    {
        for (int i = 0; i < NPC.Length; i++)
        {
            NPC[i].talkPlane.SetActive(false);
        }
        NPC[a].talkPlane.SetActive(true);
    }
}
